package com.yc.http.v1.impl;

import com.yc.http.v1.*;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/session.s")
public class SessionServlet extends HttpServletImpl {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        final HttpSession session = request.getSession();
        response.setContentType("text/html;charset=utf-8");
        final PrintWriter out = response.getWriter();

        // 验证会话中的数据
        final Object sname = session.getAttribute("name");

        out.append("<h1>").append(""+sname).append("<h1>").append(session.getId());

        // 存入数据到会话
        final String name = request.getParameter("name");
        if(name!= null){
            session.setAttribute("name",name);
        }
    }
}
